<?php

// 项目管理
class IndexController extends AdminController
{
    // 项目列表
    public function listAction()
    {
        $do = $this->get('do');
        switch ($do) {
            case 'list':
                $parameters['conditions'] = array();
                $data = $this->_list(DunProjectsModel::class, $parameters);
                foreach ($data['data'] as $item) {
                    $data_result[] = array(
                        'id' => $item->id,
                        'name' => $item->name,
                        'description' => $item->description,
                        'domains' => $item->domains,
                        'servers' => $item->servers,
                        'sources' => $item->sources,
                        'create_time' => $item->create_time,
                        'start_time' => $item->start_time,
                        'end_time' => $item->end_time,
                        'admin_userid' => $item->admin_userid,
                    );
                }
                $this->ajaxresult(0, '', $data_result, array('count' => $data['count']));
                break;
        }
    }

    // 项目http配置
    function httpAction()
    {
        $do = $this->get('do');
        $projectid = $this->get('projectid');
        switch ($do) {
            case 'list':
                $parameters['conditions'] = [
                    'projectid' => $projectid
                ];
                $data = $this->_list(DunProjectSiteModel::class, $parameters);
                foreach ($data['data'] as $item) {
                    $data_result[] = array(
                        'id' => $item->id,
                        'projectid' => $item->projectid,
                        'project_name' => $item->project->name,
                        'domain' => $item->domain,
                        'source_ip' => $item->source_ip,
                        'target_port' => $item->target_port,
                        'source_port' => $item->source_port,
                        'publicKey' => $item->publicKey,
                        'privateKey' => $item->privateKey,
                    );
                }
                $this->ajaxresult(0, '', $data_result, array('count' => $data['count']));
                break;
        }
        $this->view->assign('projectid',$projectid);
    }

    // site配置
    function savehttpAction()
    {
        $projectid = $this->get('projectid');
        $this->view->assign('projectid',$projectid);
        $id = intval($this->request->getParam('id'));
        if ($id) {
            $data = DunProjectSiteModel::find($id);
            if (!$data) $this->ajaxresult(0, '未找到您要修改的信息');
            $this->view->assign('data', $data);
        }
        if ($this->ispost) {
            $post_data = array(
                'projectid' => $this->post('projectid'),
                'domain' => $this->post('domain'),
                'source_ip' => $this->post('source_ip'),
                'target_port' => $this->post('target_port'),
                'source_port' => $this->post('source_port'),
            );
            if ($data) {
                $status = $data->save($post_data);
            } else {
                $data = new DunProjectSiteModel();
                $status = $data->save($post_data);
            }
            if ($status == false) $this->ajaxresult(1, '添加失败');
            $this->ajaxresult(0, '处理成功');
        }
        $this->view->assign('table', $this->get('table'));
    }

    function savetcpAction()
    {
        $projectid = $this->get('projectid');
        $id = intval($this->request->getParam('id'));
        if ($id) {
            $data = DunProjectTcpModel::find($id);
            if (!$data) $this->ajaxresult(0, '未找到您要修改的信息');
            $this->view->assign('data', $data);
        }
        if ($this->ispost) {
            $post_data = array(
                'projectid' => $this->post('projectid'),
                'source_ip' => $this->post('source_ip'),
                'target_port' => $this->post('target_port'),
                'source_port' => $this->post('source_port'),
            );
            if ($data) {
                $status = $data->save($post_data);
            } else {
                $data = new DunProjectTcpModel();
                $status = $data->save($post_data);
            }
            if ($status == false) $this->ajaxresult(1, '添加失败');
            $this->ajaxresult(0, '处理成功');
        }
        $this->view->assign('table', $this->get('table'));
        $this->view->assign('projectid',$projectid);
    }

    // tcp配置
    function tcpAction()
    {
        $do = $this->get('do');
        $projectid = $this->get('projectid');
        switch ($do) {
            case 'list':
                $parameters['conditions'] = [
                    'projectid' => $projectid
                ];
                $data = $this->_list(DunProjectTcpModel::class, $parameters);
                foreach ($data['data'] as $item) {
                    $data_result[] = array(
                        'id' => $item->id,
                        'projectid' => $item->projectid,
                        'project_name' => $item->project->name,
                        'source_ip' => $item->source_ip,
                        'target_port' => $item->target_port,
                        'source_port' => $item->source_port,
                    );
                }
                $this->ajaxresult(0, '', $data_result, array('count' => $data['count']));
                break;
        }
        $this->view->assign('projectid',$projectid);
    }

    // 新增编辑
    function addAction()
    {
        $id = intval($this->request->getParam('id'));
        if ($id) {
            $data = DunProjectsModel::find($id);
            if (!$data) $this->ajaxresult(0, '未找到您要修改的信息');
            $this->view->assign('data', $data);
        }
        if ($this->ispost) {
            $post_data = array(
                'name' => $this->post('name'),
                'description' => $this->post('description'),
                'sources' => $this->post('sources'),
                'create_time' => date('Y-m-d H:i:s'),
                'start_time' => $this->post('start_time'),
                'end_time' => $this->post('end_time'),
            );
            if ($data) {
                $status = $data->save($post_data);
            } else {
                $post_data['domains'] = rand(10000, 99999) . '.' . Helper::hash_orderid(rand(10000, 99999)) . '.fanghu.com';
                $data = new DunProjectsModel();
                $status = $data->save($post_data);
            }
            if ($status == false) $this->ajaxresult(1, '添加失败');
            $this->ajaxresult(0, '处理成功');
        }
    }

    // 项目流量
    function trafficAction()
    {

    }

    // 刷新域名解析
    function refreshdomainAction()
    {

    }

    // 节点配置
    function serversAction()
    {
    }
}